# coding: utf-8
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl

mpl.rcParams['font.family'] = 'sans-serif'
mpl.rcParams['font.sans-serif'] = 'NSimSun,Times New Roman'

# function draw 1 pic
## input: line col no. numOfLabels numOfTimes
## output: none

def drawpic(pline, pcol, No, numOfLabels, numOfTimes):
    """"
    for each file input
    output one pic on designed position
    """
    plt.subplot(pline, pcol, No)
    filecenter = 'output-' + str(numOfLabels) + str(numOfTimes) + '/cluster-' + str(numOfTimes) + '/part-r-00000'
    with open(filecenter) as fcenter:
        linescenter = fcenter.readlines()
    loccenter = []
    for l in linescenter:
        l = l.rstrip()
        loc_tmp = l.split(',')
        loc_tmp = [np.double(x) for x in loc_tmp]
        loccenter.append(loc_tmp)
    loccenter = np.array(loccenter)
    plt.plot(loccenter[:, 2], loccenter[:, 3], '*', label='centers', color='r', ms=10)
    plt.xlabel('x')
    plt.ylabel('y')
    # plt.axis('equal')
    # plt.xlim(0,100)
    # plt.ylim(0,100)
    ptitle = 'Data-' + str(numOfLabels) + '-' + str(numOfTimes)
    plt.title(ptitle)
    plt.legend()

    filescatter = 'output-' + str(numOfLabels) + str(numOfTimes) + '/clusteredInstances/part-m-00000'
    with open(filescatter) as fscatter:
        linesscatter = fscatter.readlines()
    numOfLines = len(linesscatter)
    location = np.zeros((numOfLines, 2))
    locLabels = []
    index = 0
    for l in linesscatter:
        l = l.strip()
        ListFromLine = l.split('\t')
        location[index, :] = ListFromLine[0].split(',')
        locLabels.append((int(ListFromLine[1])))
        index += 1
    plt.scatter(location[:, 0], location[:, 1], c=locLabels)
    return None


drawpic(1, 1, 1, 3, 5)
plt.show()
drawpic(1, 1, 1, 3, 10)
plt.show()
drawpic(1, 1, 1, 3, 20)
plt.show()
drawpic(1, 1, 1, 3, 50)
plt.show()
drawpic(1, 1, 1, 5, 5)
plt.show()
drawpic(1, 1, 1, 5, 10)
plt.show()
drawpic(1, 1, 1, 5, 20)
plt.show()
drawpic(1, 1, 1, 5, 50)
plt.show()
drawpic(1, 1, 1, 10, 5)
plt.show()
drawpic(1, 1, 1, 10, 10)
plt.show()
